import 'package:flutter/material.dart';
import 'package:avd_sdk_plugin/avd_sdk_plugin.dart';
import 'meeting.dart';
import 'meeting_display.dart';
import 'package:avd_sdk_plugin/avd_room_info.dart';


class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();

}

class _LoginPageState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  final _usernameController = TextEditingController(text:'FlutterTest');
  final _roomidController = TextEditingController(text: '10000000000');
  bool _isLoading = false;

  @override
  void initState() {
    super.initState();
    initAVDPlugin();
  }

  Future<void> initAVDPlugin() async{
    AvdSdkPlugin.init('https://avd.nice2meet.cn:9610', 'demo_access', 'demo_secret');
    AvdSdkPlugin.onInitResult = (int? result){
      print('onInitResult = $result');

    };
    String? version = await AvdSdkPlugin.getVersion();
    print('叁体 getVersion = $version');
  }

  @override
  Widget build(BuildContext context) {

  return MaterialApp(
        home:Scaffold(
      appBar: AppBar(
        title: Text('PluginTest'),
      ),
      body: Padding(
        padding: EdgeInsets.all(16.0),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: <Widget>[
              TextFormField(
                controller: _roomidController,
                decoration: InputDecoration(
                  labelText: '房间号',
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Please enter a username';
                  }
                  return null;
                },
              ),
              SizedBox(height: 8.0),
              TextFormField(
                controller: _usernameController,
                //obscureText: true,
                decoration: InputDecoration(
                  labelText: '用户名',
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return 'Please enter a password';
                  }
                  return null;
                },
              ),
              SizedBox(height: 16.0),
              ElevatedButton(
                onPressed: _isLoading ? null : _login,
                child: _isLoading
                    ? CircularProgressIndicator()
                    : Text('加入房间'),
              ),
              ElevatedButton(
                onPressed: _isLoading ? null : _creatRoom,
                child: _isLoading
                    ? CircularProgressIndicator()
                    : Text('创建房间'),
              ),
            ],
          ),
        ),
      ),
     ),
    );
  }

  void _login() async{
    setState(() {

    });
    AvdSdkPlugin.joinRoom(_roomidController.text, _usernameController.text, _usernameController.text, '');
    AvdSdkPlugin.onJoinResult = (int? result){
      if (result == 0){
        //AvdSdkPlugin.publishCamera();

      }
       Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => MeetingDisplay(),
        ),
      );
    };

    // if (_formKey.currentState!.validate()) {
    //   setState(() {
    //     _isLoading = true;
    //   });
    // }
  }

  void _creatRoom() async{
    AVDRoomInfo info = AVDRoomInfo();
    info.roomId = _roomidController.text;
    info.owerId = 'uuid';//这里最后填uuid保障唯一性
    info.roomName = '这是一个flutter测试创建房间的';
    info.roomTopic = '这是一个flutter测试创建房间的';
    AvdSdkPlugin.scheduleRoom(info);
    AvdSdkPlugin.onScheduleRoomResult = (int result, String roomId){
         print('plugin creatRoom code = $result room = $roomId');
         if(result == 0){
           print('plugin creatRoom succeed');
         }else{
           print('plugin creatRoom fail args error');
         }
    };

   }
}
